#! /usr/bin/python
# -*- coding: utf-8 -*-

import os
import sys
import time
import datetime

import global_var

log = global_var.gl_log

class CPlateDataSyn():
    def __init__(self,que_plate=global_var.gl_que_plate):#输入参数 plate 队列
        self.q = que_plate

    def get_oid(self, pid):
        ret = None
        try:
            if len(self.q):
                for _it in self.q:
                    if _it["pid"]==pid:
                        ret = _it["oid"]
                        break
        except Exception as e:
            ret = None
            print("error: ", e)

        return ret
    def set_state(self, oid, state):
        try:
            for _it in self.q:
                if _it["oid"]==oid:
                    _it["state"]=state
                    break

        except Exception as e:
            pass
    
    def clear(self):#remove the state = "shaped", 已收到板形
        try:
            for _it in self.q:
                if _it["state"]=="shaped":
                    self.q.remove(_it)

        except Exception as e:
            pass
    def insert(self, pid, oid,state="calc1"):
        ret = True
        try:

            
            for _it in self.q:
                if _it["oid"]==oid:
                    return False

            _data = {"pid":pid, "oid":oid,"time":int(time.time()),"state":state}
            self.q.append(_data)

            ret = True

        except Exception as e:
            ret = False
        
        return ret
    
    #remove the first 
    def remove(self, pid):
        ret = True
        try:
            for _it in self.q:
                if _it["pid"]==pid:
                    q.remove(_it)

        except Exception as e:
            ret = False

        return ret

    ######## 
    def pop(self, pid):
        ret = None
        try:
            while len(self.q):
                _it = self.q.pop()
                if self.q["pid"]==pid:
                    ret= _it
                    break
        except Exception as e:
            pass

        return ret